// pages/integralTask/integralTask.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isNone: false,
    isSign: false
  },
  onLoad: function() {
    wx.showShareMenu({
      // 要求小程序返回分享目标信息
      withShareTicket: true
    }); 
    this.init();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.init();
  },

  //获取数据
  init(cb){
    wx.showLoading({ title: '加载中...' });
    wx.request({
      url: app.baseURL + 'Integral&a=task',
      data: {
        user_id: app.globalData.userId
      },
      success: (res) => {
        if (res.statusCode == 200) {
          this.setData({
            info: res.data,
          });
          wx.hideLoading();
          // 如果有回调函数执行回调
          typeof cb == 'function' && cb();
        }
      }
    });
  },

  //点击签到
  clicksign() {
    wx.showLoading({ title: "加载中..." });
    wx.request({
      url: app.baseURL + 'Integral&a=sign',
      data: {
        user_id: app.globalData.userId
      },
      success: (res) => {
        if (res.data.status == 0){
          wx.showModal({
            title: '温馨提示',
            content: '今天已经签到过了,明天再来哦~',
          })
          wx.hideLoading();
        }
        if (res.data.status == 1) {
          this.setData({
            isSign: true,
            signData: res.data,
            'info.is_sign': "1",
          });
          
        }
        this.init();
        wx.hideLoading();
      }
    });
  
  },
  hideSign() {  //隐藏签到提示
    this.setData({
      isSign: false
    })
  },

  // 转发
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      if (res.target.dataset.type == 0) {   //转发好友
        return {
          title: app.globalData.title,
          path: '/pages/index/index?other_user_id=' + app.globalData.userId,
          imageUrl: 'https://jrtest.0791jr.com/WeChat/images/integralTaskshare.png'
        }
      }
      if (res.target.dataset.type == 1) {   //转发群聊
        return {
          title: app.globalData.title,
          path: '/pages/index/index?other_user_id=' + app.globalData.userId,
          imageUrl: 'https://jrtest.0791jr.com/WeChat/images/integralTaskshare.png',
          success: res => {
            console.log(res);
            if (res.shareTickets){
              wx.getShareInfo({
                shareTicket: res.shareTickets[0],
                success: res => {
                  wx.request({
                    url: app.baseURL + 'share&a=decode',
                    data: {
                      user_id: app.globalData.userId,
                      encryptedData: res.encryptedData,
                      iv: res.iv
                    },
                    success: res => {
                      this.init();
                    },
                    fail: function (res) { },
                  })
                },
                fail: function (res) {
                  console.log(res)
                }
              })
            }else{
              res.shareTickets || wx.showModal({
                title: '温馨提示',
                content: '需分享至微信群',
              });
            }
          },
          fail: function (res) {
            // 分享失败
            console.log(res)
          }
        }
      }
    };
  },
  //下拉刷新
  onPullDownRefresh() {
    this.init(wx.stopPullDownRefresh());
  },
})